// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © EMA34TRADER
//@version=6
indicator("Trinity ADX + EMA Scalper", shorttitle="Trinity ADX EMA", overlay=true)

// ── ADX Inputs ─────────────────────
adxLen     = input.int(14,  title="ADX & DI Length", minval=1)
level20    = input.int(15,  title="White Background when ADX >", minval=1, maxval=50)
level25    = input.int(20,  title="Strong Color when ADX >", minval=1, maxval=100)

bullColor  = input.color(color.new(color.green, 88), title="Bullish Background (>25)")
bearColor  = input.color(color.new(color.red,   88), title="Bearish Background (>25)")
whiteBg    = input.color(color.new(color.white, 92), title="White Background (>20)")

showLines  = input.bool(false, title="Show +DI / -DI / ADX Lines (may distort scale)")
showBg     = input.bool(true,  title="Show ADX Background")

diPlusCol  = input.color(color.green,  title="+DI Color")
diMinusCol = input.color(color.red,    title="-DI Color")
adxCol     = input.color(color.yellow, title="ADX Color")

// ── EMA Scalper Inputs ─────────────────────
emaLen     = input.int(20, minval=1, title="EMA Length")
src        = input.source(close, title="Source")
hl_len     = input.int(8, minval=1, title="High/Low Length")

emaColor        = input.color(color.blue,  title="EMA Color")
highColor       = input.color(color.red,   title="Highest Color")
lowColor        = input.color(color.green, title="Lowest Color")
buyArrowColor   = input.color(color.green, title="Buy Arrow Color")
sellArrowColor  = input.color(color.red,   title="Sell Arrow Color")
buyTextColor    = input.color(color.green, title="Buy Text Color")
sellTextColor   = input.color(color.red,   title="Sell Text Color")

showEmaPlots    = input.bool(true, title="Show EMA / Highest / Lowest")
showSignals     = input.bool(true, title="Show Buy / Sell Arrows")
useAdxFilter    = input.bool(false, title="Filter Signals with ADX Trend")
onlyStrongAdx   = input.bool(false, title="Only Signal when ADX > Strong Level")
arrowSize       = input.string("tiny", title="Arrow Size", options=["tiny", "small", "normal"])

showAdxLabel    = input.bool(true, title="Show ADX Status Label")

// ── ADX Calculation ─────────────────────
TrueRange                  = math.max(math.max(high - low, math.abs(high - nz(close[1]))), math.abs(low - nz(close[1])))
DirectionalMovementPlus    = high - nz(high[1]) > nz(low[1]) - low ? math.max(high - nz(high[1]), 0) : 0
DirectionalMovementMinus   = nz(low[1]) - low > high - nz(high[1]) ? math.max(nz(low[1]) - low, 0) : 0

var float SmoothedTrueRange                = na
var float SmoothedDirectionalMovementPlus  = na
var float SmoothedDirectionalMovementMinus = na

SmoothedTrueRange                := nz(SmoothedTrueRange[1]) - (nz(SmoothedTrueRange[1]) / adxLen) + TrueRange
SmoothedDirectionalMovementPlus  := nz(SmoothedDirectionalMovementPlus[1]) - (nz(SmoothedDirectionalMovementPlus[1]) / adxLen) + DirectionalMovementPlus
SmoothedDirectionalMovementMinus := nz(SmoothedDirectionalMovementMinus[1]) - (nz(SmoothedDirectionalMovementMinus[1]) / adxLen) + DirectionalMovementMinus

DIPlus  = SmoothedTrueRange > 0 ? SmoothedDirectionalMovementPlus  / SmoothedTrueRange * 100 : na
DIMinus = SmoothedTrueRange > 0 ? SmoothedDirectionalMovementMinus / SmoothedTrueRange * 100 : na
diSum   = DIPlus + DIMinus
DX      = diSum > 0 ? math.abs(DIPlus - DIMinus) / diSum * 100 : na
ADX     = ta.sma(DX, adxLen)

// ── EMA Scalper Calculations ─────────────────────
out    = ta.ema(src, emaLen)
lastH  = ta.highest(close, hl_len)
lastL  = ta.lowest(close, hl_len)

// ── Direction & Background ─────────────────────
isBull = DIPlus > DIMinus
isBear = DIMinus > DIPlus

color bgColor = na
if showBg
    if ADX > level25 and isBull
        bgColor := bullColor
    else if ADX > level25 and isBear
        bgColor := bearColor
    else if ADX > level20
        bgColor := whiteBg

bgcolor(bgColor, title="ADX Trend Background")

// ── ADX Lines (optional) ─────────────────────
plot(showLines ? DIPlus  : na, color=diPlusCol,  title="+DI", linewidth=2)
plot(showLines ? DIMinus : na, color=diMinusCol, title="-DI", linewidth=2)
plot(showLines ? ADX     : na, color=adxCol,     title="ADX", linewidth=2)

hline(showLines ? level20 : na, "Level 20", color=color.gray)
hline(showLines ? level25 : na, "Level 25", color=color.white)

// ── EMA / High / Low Plots ─────────────────────
plot(showEmaPlots ? out   : na, title="EMA",     color=emaColor)
plot(showEmaPlots ? lastH : na, title="Highest", color=highColor, linewidth=2)
plot(showEmaPlots ? lastL : na, title="Lowest",  color=lowColor,  linewidth=2)

// ── Buy / Sell Signals ─────────────────────
rawBearish = ta.cross(close, out) and close[1] > close
rawBullish = ta.cross(close, out) and close[1] < close

adxOkBull = not useAdxFilter or (isBull and (not onlyStrongAdx or ADX > level25))
adxOkBear = not useAdxFilter or (isBear and (not onlyStrongAdx or ADX > level25))

bearish = showSignals and rawBearish and adxOkBear
bullish = showSignals and rawBullish and adxOkBull

// ── Plotshapes with constant sizes (fixes the error) ─────────────────────
// Sell arrows
plotshape(bearish and arrowSize == "tiny",   style=shape.arrowdown, location=location.abovebar, color=sellArrowColor, text="Sell", textcolor=sellTextColor, size=size.tiny)
plotshape(bearish and arrowSize == "small",  style=shape.arrowdown, location=location.abovebar, color=sellArrowColor, text="Sell", textcolor=sellTextColor, size=size.small)
plotshape(bearish and arrowSize == "normal", style=shape.arrowdown, location=location.abovebar, color=sellArrowColor, text="Sell", textcolor=sellTextColor, size=size.normal)

// Buy arrows
plotshape(bullish and arrowSize == "tiny",   style=shape.arrowup, location=location.belowbar, color=buyArrowColor, text="Buy", textcolor=buyTextColor, size=size.tiny)
plotshape(bullish and arrowSize == "small",  style=shape.arrowup, location=location.belowbar, color=buyArrowColor, text="Buy", textcolor=buyTextColor, size=size.small)
plotshape(bullish and arrowSize == "normal", style=shape.arrowup, location=location.belowbar, color=buyArrowColor, text="Buy", textcolor=buyTextColor, size=size.normal)

// ── Alerts ─────────────────────
alertcondition(bullish, title="Trinity Buy",  message="Trinity ADX EMA: Buy Signal")
alertcondition(bearish, title="Trinity Sell", message="Trinity ADX EMA: Sell Signal")

// ── ADX Status Label ─────────────────────
if showAdxLabel and barstate.islast
    string dir = isBull ? "Bull" : isBear ? "Bear" : "Neutral"
    color lblCol = isBull ? color.green : isBear ? color.red : color.gray
    string txt = "ADX: " + str.tostring(ADX, "#.#") + "\n" + dir
    label.new(bar_index, high, txt, 
         style=label.style_label_down, color=color.new(lblCol, 20), 
         textcolor=color.white, size=size.small)
